package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
@Transactional
    public void insert(Role role, Long[] permissionIds) {
        roleMapper.insert(role);
    }
    @Transactional
    @Override
    public void update(Role role,Long[] permissionIds) {

        roleMapper.update(role);
        roleMapper.deleteRelation(role.getId());
        if (permissionIds.length>0&&permissionIds!=null) {
            for (Long permissionId : permissionIds) {
                roleMapper.insertRelation(role.getId(),permissionId);
            }}
    }
    @Transactional
    @Override
    public void delete(Long id) {
roleMapper.delete(id);
    }
    @Transactional
    @Override
    public Role selectOne(Long id) {

        return roleMapper.selectForId(id);
    }
    @Transactional
    @Override
    public List<Role> selectList() {
        return roleMapper.selectList();
    }
    @Transactional
    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
            List<Role> list = roleMapper.selectForList(qo);
            return new PageInfo(list);
        }

    @Override
    public List<Role> queryByEmployeeId(Long employeeId) {

        return roleMapper.selectByEmployeeId(employeeId);
    }

}